<template>
  <div class="app-container">
    <h2 class="con-title">售后详情</h2>
    <div class="detail-btn-box">
      <el-button type="primary" v-if="list.returnStatus==3&&list.receiveGoodsStatus==1&&list.returnType==2" @click="delivery()">确认收货</el-button>
      <el-button type="primary" v-if="list.returnStatus==1&&list.receiveGoodsStatus==1&&list.returnType==2" @click="returnMoney()">审核通过</el-button>
      <el-button type="primary" v-if="list.returnStatus==1&&list.receiveGoodsStatus==1&&list.returnType==2" @click="refuse()">拒绝</el-button>
    </div>
    <div class="tabel-index">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-title">售后基础信息</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">售后编号</div>
        </el-col>
        <el-col :span="20">
          <div class="grid-content grid-left">{{list.returnCode}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">售后状态</div>
        </el-col>
        <el-col :span="20">
          <div class="grid-content grid-left" style="color:#F00;" v-if="list.returnType == 1">{{list.returnStatus|returnStatus2}} <span v-if="list.returnStatus==5">{{list.refusalReason}}</span></div>
          <div class="grid-content grid-left" style="color:#F00;" v-if="list.returnType == 2">{{list.returnStatus|returnStatus1}} <span v-if="list.returnStatus==5">{{list.refusalReason}}</span></div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">申请时间</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left" v-if="list.createDate!=null">{{list.createDate|formatDate( 'yyyy-MM-dd hh:mm')}}</div>
          <div class="grid-content grid-left" v-else></div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">审核时间</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left" v-if="list.checkDate!=null">{{list.checkDate|formatDate( 'yyyy-MM-dd hh:mm')}}</div>
          <div class="grid-content grid-left" v-else></div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">撤销时间</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left" v-if="list.revokeDate!=null">{{list.revokeDate|formatDate( 'yyyy-MM-dd hh:mm')}}</div>
          <div class="grid-content grid-left" v-else></div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">完成时间</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left" v-if="list.completeDate!=null">{{list.completeDate|formatDate( 'yyyy-MM-dd hh:mm')}}</div>
          <div class="grid-content grid-left" v-else></div>
        </el-col>
      </el-row>
    </div>
    <div class="tabel-index">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-title">退货信息</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">退货原因</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.returnReason}}</div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">售后类型</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.returnType | returnType}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">退款金额</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">¥{{list.returnPrice|numFilter}}</div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">退运费</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">¥{{list.returnFreight|numFilter}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">退款说明</div>
        </el-col>
        <el-col :span="20">
          <div class="grid-content grid-left">{{list.returnNote}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content img-content">图片凭证</div>
        </el-col>
        <el-col :span="20">
          <div class="grid-content grid-left img-content">
            <img :src="item.returnImg" alt="" v-for="(item,index) in list.returnImgList" :key='item.id' :class="{'active':index==current}"  @click="isClick(item,index)">
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="tabel-index">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-title">关联订单信息</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">订单编号</div>
        </el-col>
        <el-col :span="20">
          <div class="grid-content grid-left">{{list.orderCode}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">下单时间</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left" v-if="list.orderCreateDate!=null">{{list.orderCreateDate|formatDate( 'yyyy-MM-dd hh:mm')}}</div>
          <div class="grid-content grid-left" v-else></div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">支付时间</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left" v-if="list.orderPayDate!=null">{{list.orderPayDate|formatDate( 'yyyy-MM-dd hh:mm')}}</div>
          <div class="grid-content grid-left" v-else></div>
        </el-col>
      </el-row>
    </div>
    <div class="tabel-index">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-title">订单配送信息</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">收货人</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.receiverName}}</div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">配送物流</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.expressName}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">省市区</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">
            {{list.provinceName}} {{list.cityName}} {{list.areaName}}
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">运单号</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.expressNumber}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">详细地址</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.addressName}}</div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">邮编</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.zipCode}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">联系电话</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.orderTel}}</div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content">手机</div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content grid-left">{{list.orderPhone}}</div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4">
          <div class="grid-content">买家留言</div>
        </el-col>
        <el-col :span="20">
          <div class="grid-content grid-left" v-if="list.orderRemark == 'null'"></div>
          <div class="grid-content grid-left" v-else>{{list.orderRemark}}</div>
        </el-col>
      </el-row>
    </div>
    <div class="tabel-index">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-title">售后商品信息</div>
        </el-col>
      </el-row>
      <el-table :data="returnGoodsList" v-loading.body="listLoading" stripe element-loading-text="Loading" border highlight-current-row>
        <el-table-column align="center" label='商品' min-width="200px">
          <template slot-scope="scope">
            <div class="goods">
              <img :src="scope.row.goodsImg" alt="">
              <div class="right">
                <h3>{{scope.row.goodsName}}</h3>
                <p>商品编号：{{scope.row.goodsCode}}</p>
                <p>规格：{{scope.row.goodsSpec}}</p>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="价格(元)" align="center">
          <template slot-scope="scope">
            {{scope.row.goodsDiscountPrice|numFilter}}
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.goodsNum}}</span>
          </template>
        </el-table-column>
        <el-table-column label="小记(元)" align="center">
          <template slot-scope="scope">
            {{scope.row.totalGoodsPrice|numFilter}}
          </template>
        </el-table-column>
        <!--<el-table-column align="center" label='佣金'>
                    <template slot-scope="scope">
                      {{scope.row.sku}}
                    </template>
                  </el-table-column>-->
        <el-table-column align="center" label='订单状态'>
          <template slot-scope="scope">
            {{scope.row.orderGoodsStatus|formatStatus}}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="退款金额" :visible.sync="dialogExpressVisible" width="35%" center>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="退款金额：" :label-width="formLabelWidth">
          ¥{{list.returnPrice|numFilter}} 元
        </el-form-item>
        <el-form-item label="退货地址：" :label-width="formLabelWidth" style="overflow:hidden;" prop="radio">
          <el-radio-group v-model="form.radio">
            <el-radio :label="0" style="margin-left:0px;margin-top:15px;display:block;">默认仓库地址</el-radio>
            <el-radio :label="item.id" v-for="item in options" :key="item.name" style="margin-left:0px;margin-top:15px;display:block;white-space: normal;">{{item.warehouseName+'('+item.returnAddress+')'}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <el-button type="warning" size="small" style="margin-left:120px" @click="dialogAddressVisible = true">新增退货地址</el-button>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogExpressVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="新增地址" :visible.sync="dialogAddressVisible" width="35%" center>
      <el-form :model="addressForm" :rules="addressRules" ref="addressForm">
        <el-form-item label="仓库名称：" :label-width="formLabelWidth" prop="warehouseName">
          <el-input v-model="addressForm.warehouseName" auto-complete="off" width="200px;"></el-input>
        </el-form-item>
        <el-form-item label="省市区：" :label-width="formLabelWidth" prop="areaSelect">
          <el-cascader :options="optionsAdd" v-model="addressForm.areaSelect" @change="handleChange" placeholder="请选择省市区">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址：" :label-width="formLabelWidth" prop="returnAddress">
          <el-input v-model="addressForm.returnAddress" auto-complete="off" width="200px;"></el-input>
        </el-form-item>
        <el-form-item label="退货人：" :label-width="formLabelWidth" prop="returnLinkman">
          <el-input v-model="addressForm.returnLinkman" auto-complete="off" width="200px;"></el-input>
        </el-form-item>
        <el-form-item label="退货人手机号：" :label-width="formLabelWidth" prop="returnPhone">
          <el-input v-model="addressForm.returnPhone" auto-complete="off" width="200px;"></el-input>
        </el-form-item>
        <el-form-item label="邮政编码：" :label-width="formLabelWidth" prop="returnZipCode">
          <el-input v-model="addressForm.returnZipCode" auto-complete="off" width="200px;"></el-input>
        </el-form-item>
        <el-form-item label="备注：" :label-width="formLabelWidth" prop="returnRemark">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="addressForm.returnRemark">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSuccess('addressForm')">确 定</el-button>
      </div>
    </el-dialog>
    <transition-group name="fade" tag="div">
        <div class="imgScale" v-if="flag" key="1">
          <img :src="src" alt="" v-if="flag">
        </div>
      </transition-group>
      <transition-group name="fade" tag="div">
        <div class='popContainer' v-if="flag" key="2" @click="()=>flag=false"></div>
      </transition-group>
  </div>
</template>

<script>
import {
  getOrderReturnDetails
} from '@/api/list'
import {
  getSupplierReturnAddressList
} from '@/api/list'
import {
  addSupplierReturnAddress
} from '@/api/list'
import {
  checkOrderReturnPass
} from '@/api/list'
import {
  checkOrderReturnRefuse
} from '@/api/list'
import {
  confirmReceiveGoods
} from '@/api/list'
import validate from '@/utils/el-validate'
import {
  regionData,
  CodeToText
} from 'element-china-area-data'

export default {
  data() {
    return {
      current: null,
      flag: false,
      src: '',
      list: {},
      returnGoodsList: [],
      listLoading: true,
      returnId: this.$route.query.returnId,
      form: {
        expressNumber: '',
        radio: 0
      },
      addressForm: {
        warehouseName: '',
        areaSelect: [],
        returnLinkman: '',
        returnPhone: '',
        returnZipCode: '',
        returnProvince: '',
        returnCity: '',
        returnArea: '',
        returnAddress: '',
        returnRemark: ''
      },
      optionsAdd: regionData,
      dialogExpressVisible: false,
      dialogAddressVisible: false,
      options: [{
        id: '0',
        warehouseName: '',
        returnAddress: '默认仓库地址'
      }],
      formLabelWidth: '120px',
      rules: {
        radio: [
          { required: true, message: '请选择退货地址', trigger: 'change' }
        ]
      },
      addressRules: {
        areaSelect: [{
          type: 'array',
          required: true,
          message: '请选择省市区',
          trigger: 'trigger'
        }],
        returnAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ],
        returnLinkman: [
          { required: true, message: '请输入退货人姓名', trigger: 'blur' }
        ],
        warehouseName: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ],
        returnZipCode: [
          { required: true, message: '请输入邮政编码', trigger: 'blur' }
        ],
        returnPhone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }, {
            trigger: 'blur',
            validator: validate('telephone', '请输入正确的手机格式')
          }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  filters: {

    numFilter(value) {
      const realVal = Number(value)
      return realVal.toFixed(2)
    },
    returnStatus1(val) {
      return val === 1 ? '买家提交了退货退款申请，请尽快处理' : val === 2 ? '售后申请已审核，等待买家退货' : val === 3 ? '买家已退回货品，等待确认收货' : val === 4 ? '已确认' : val === 5 ? '售后申请拒绝，拒绝原因：' : val === 6 ? '买家撤销了售后申请' : val === 7 ? '已确认收到买家退货，等待头号买手平台退款' : val === 8 ? '头号买手平台已退款，售后申请完成。' : '未知'
    },
    returnStatus2(val) {
      return val === 1 ? '买家提交了退款申请，等待头号买手平台处理' : val === 5 ? '售后申请拒绝，拒绝原因：' : val === 6 ? '买家撤销了售后申请' : val === 7 ? '售后申请审核通过，等待头号买手平台退款' : val === 8 ? '头号买手平台已退款，售后申请完成。' : '未知'
    },
    formatStatus(val) {
      return val === 1 ? '待付款' : val === 2 ? '待发货' : val === 3 ? '已发货' : val === 4 ? '已完成' : val === 5 ? '交易关闭' : '未知'
    },
    returnType(val) {
      return val === 1 ? '退款' : val === 2 ? '退款退货' : '未知'
    }
  },
  methods: {
    showCreatedTimes() {
      this.createdTimes = this.createdTimes + 1
    },
    fetchData() {
      this.listLoading = true
      getOrderReturnDetails({
        returnId: this.returnId
      }).then(res => {
        this.list = res.data.returnDetails
        this.returnGoodsList = res.data.returnDetails.returnGoodsList
        this.listLoading = false
      })
    },
    delivery() {
      this.$confirm('是否确认收货?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        confirmReceiveGoods({
          returnId: this.returnId
        }).then(res => {
          this.$message({
            type: 'success',
            message: '确认收货成功!'
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消确认收货'
        })
      })
    },
    handleAdd() {
      this.dialogAddressVisible = true
    },
    handleChange(value) {
      // 地区代码转成汉字
      this.addressForm.returnProvince = CodeToText[value[0]]
      this.addressForm.returnCity = CodeToText[value[1]]
      this.addressForm.returnArea = CodeToText[value[2]]
    },
    returnMoney() {
      this.dialogExpressVisible = true
      getSupplierReturnAddressList().then(res => {
        this.options = res.data.returnAddressList
      })
    },
    addSuccess(addressForm) {
      this.$refs[addressForm].validate((valid) => {
        if (valid) {
          addSupplierReturnAddress({ warehouseName: encodeURI(this.addressForm.warehouseName), returnProvince: encodeURI(this.addressForm.returnProvince), returnCity: encodeURI(this.addressForm.returnCity), returnArea: encodeURI(this.addressForm.returnArea), returnAddress: encodeURI(this.addressForm.returnAddress), returnLinkman: encodeURI(this.addressForm.returnLinkman), returnPhone: this.addressForm.returnPhone, returnZipCode: this.addressForm.returnZipCode, returnRemark: encodeURI(this.addressForm.returnRemark) }).then(res => {
            this.$message({
              message: '新增成功',
              type: 'success'
            })
            this.returnMoney()
            this.dialogAddressVisible = false
            this.$refs[addressForm].resetFields()
          })
        } else {
          return false
        }
      })
    },
    submit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          checkOrderReturnPass({ returnId: this.returnId, returnAddressId: this.form.radio }).then(res => {
            this.$message({
              message: '审核通过',
              type: 'success'
            })
            this.dialogExpressVisible = false
            this.fetchData()
          })
        } else {
          return false
        }
      })
    },
    refuse() {
      this.$prompt('请输入拒绝原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '请输入拒绝原因'
      }).then(({ value }) => {
        checkOrderReturnRefuse({ returnId: this.returnId, refuseReason: encodeURI(value) }).then(res => {
          this.$message({
            message: '拒绝审核通过',
            type: 'success'
          })
        })
      })
    },
    isClick(item, index) {
      this.current = index
      this.flag = true
      console.log(item.returnImg)
      this.src = item.returnImg
    }
  }
}

</script>
<style rel="stylesheet/scss" lang="scss" scoped>
@import 'src/assets/css/jx-order.scss';
.grid-content {
  // border-radius: 4px;
  min-height: 46px;
  line-height: 46px;
  font-size: 14px;
}

.img-content {
  // border-radius: 4px;
  min-height: 130px;
  line-height: 130px;
  font-size: 14px;
  img {
    width: 65px;
    float: left;
    margin-top: 30px;
    margin-right: 30px;
  }
}

.goods img {
  width: 60px;
  float: left;
  margin-top: 8px;
}

.goods .right {
  float: right;
  width: 80%;
}

.goods .right h3 {
  font-size: 13px;
  color: #333333;
  text-align: left;
  margin: 0;
}

.goods .right p {
  color: #AEAEAE;
  text-align: left;
  margin: 0;
}
.popContainer {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
  }
  .imgScale{
    overflow: hidden;
    height: 600px;
    width: 600px;
    position: absolute;
    z-index: 1;
    top: 50%;
    left: 50%;
    margin-top: -300px;
    margin-left: -300px;    
    display: table-cell;
    text-align: center;
    img{
      max-height: 600px;
      max-width: 600px;
    }
  }
</style>
